package social.p2p.ui;

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import social.p2p.controller.Socialp2pController;
import social.p2p.model.Foto;

/**
 *
 * @author cadmilo
 */
public class PhotoPanel extends javax.swing.JPanel {

    private final Foto foto;
    private final MainFrame mainFrame;

    /**
     * Creates new form PhotoPanel
     */
    public PhotoPanel(final Foto foto, final MainFrame mainFrame) {
        initComponents();
        BufferedImage bi = null;
        if (foto.getFoto() != null) {
            try {
                bi = ImageIO.read(new ByteArrayInputStream(foto.getFoto()));
            } catch (IOException ex) {
                Logger.getLogger(PhotoPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
            lblFoto.setIcon(new ImageIcon(bi.getScaledInstance(140, -100, Image.SCALE_FAST)));
        } else {
            this.remove(lblFoto);
            JButton btnDescargar = new JButton("Descargar: " + foto.getUserId());
            btnDescargar.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent ae) {
                    mainFrame.downloadFoto(foto.getUserId());
                }
            });
            JButton btnVerDetalle = new JButton("Ver Detalles");
            btnVerDetalle.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent ae) {
                    panelSelected();
                }
            });
            add(btnDescargar, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 200, 100));
            add(btnVerDetalle, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 100, 200, 100));
            lblFoto.setText(foto.getUserId());
        }
        foto.setFoto(null);
        foto.setFoto1(null);
        foto.setFoto2(null);
        this.foto = foto;
        this.mainFrame = mainFrame;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblFoto = new javax.swing.JLabel();

        setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lblFoto.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblFoto.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblFotoMouseClicked(evt);
            }
        });
        add(lblFoto, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 200, 200));
    }// </editor-fold>//GEN-END:initComponents

    private void lblFotoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblFotoMouseClicked
        panelSelected();
    }//GEN-LAST:event_lblFotoMouseClicked

    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
        panelSelected();
    }//GEN-LAST:event_formMouseClicked

    private void panelSelected() {
        mainFrame.setSelectedFoto(foto);
        System.out.println("Panel seleccionado" + foto.getId());
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel lblFoto;
    // End of variables declaration//GEN-END:variables
}
